package com.qianfeng.vhly.youshare.tasks;

import android.os.AsyncTask;
import com.qianfeng.vhly.youshare.utils.HttpTool;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 15/2/5
 * Email: vhly@163.com
 */

/**
 * 获取整个商城的分类信息
 */
public class CategoryTask extends AsyncTask<String, Integer, JSONObject> {

    /**
     * 用于调用实际接收数据的对象，通常
     * 可以认为是 Activity/Fragment 因为要进行UI更新
     */
    private TaskProcessor processor;

    public CategoryTask(TaskProcessor processor){
        this.processor = processor;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONObject ret = null;

        byte[] bytes = HttpTool.get("http://open.doushangshang.com/itemcats.php?id=0");

        if (bytes != null) {
            try {
                String str = new String(bytes, "UTF-8");

                ret = new JSONObject(str);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if(processor != null){
            processor.processResult(jsonObject);
        }
    }
}
